package com.lge.qcontact;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.lge.qcontact.utils.LocalDBHelper;



public class PhotoActivity extends Activity{
	
	private String uId;
	private String GroupName;
	private Intent returnintent;
	private Activity mContext;
	private int number;
	private Integer[] Photoimg = {
            R.drawable.image1, R.drawable.image2,
            R.drawable.image3, R.drawable.image4,
            R.drawable.image5, R.drawable.image6,
            R.drawable.image7, R.drawable.image8,
            R.drawable.image9
    };
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_photo_selection);
		Intent intent = getIntent();
		uId = intent.getStringExtra("uid");
		GroupName = intent.getStringExtra("GroupName");
		mContext = new ContactListActivity();
		GridView gridview = (GridView) findViewById(R.id.gridview);
	    gridview.setAdapter(new ImageAdapter(this));
	    
	    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				//insert db
				String sql = "update CONTACTS set photoId = "+Photoimg[position]+" where uId = '"+uId+"';";
				Log.i("send", ""+Photoimg[position]);

				SQLiteDatabase localDB = (new LocalDBHelper(PhotoActivity.this)).getWritableDatabase();

				localDB.execSQL(sql);
				Log.i("db", ""+localDB);
				//넘겨주기
				
				String sql2 = "select uName, uId, email, phoneNo, mobile, photoId from CONTACTS " +
						"where gname='" + GroupName + "';";
				
				Cursor c = localDB.rawQuery(sql2, null);
				
				while(c.moveToNext()){
				Log.i("newdb", c.getString(0));
				Log.i("newdb", c.getString(1));
				Log.i("newdb", c.getString(2));
				Log.i("newdb", c.getString(3));
				Log.i("newdb", c.getString(4));
				Log.i("newdb", ""+c.getInt(5));
				}
				returnintent = new Intent(PhotoActivity.this, ContactListActivity.class);
				returnintent.putExtra("uid", uId);
				returnintent.putExtra("groupName", GroupName);
				startActivity(returnintent);
				
			}
		});
	 }
	 
	 
	protected void onPause() {
		finish();
		super.onPause();
	}

	public class ImageAdapter extends BaseAdapter {
	     private Context mContext;
	 	private Integer[] Photoimg = {
	            R.drawable.image1, R.drawable.image2,
	            R.drawable.image3, R.drawable.image4,
	            R.drawable.image5, R.drawable.image6,
	            R.drawable.image7, R.drawable.image8,
	            R.drawable.image9
	    };
	    

	     public ImageAdapter(Context c) {
	         mContext = c;
	     }

	     public int getCount() {
	         return Photoimg.length;
	     }

	     public Object getItem(int position) {
	         return null;
	     }

	     public long getItemId(int position) {
	         return 0;
	     }

	     public View getView(int position, View convertView, ViewGroup parent) {
	         ImageView imageView;
	         if (convertView == null) { 
	             imageView = new ImageView(mContext);
	             imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
	             imageView.setScaleType(ImageView.ScaleType.FIT_XY);
	             imageView.setPadding(1, 1, 1, 1);
	         } else {
	             imageView = (ImageView) convertView;
	         }

	         imageView.setImageResource(Photoimg[position]);
	         return imageView;
	     }
	   
	 }

	}

